package com.omt.tarjimdinek.domain.user.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.log4j.Logger;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
import org.hibernate.validator.constraints.Email;

import com.google.common.base.Objects;
import com.omt.tarjimdinek.domain.islamicLibrary.entity.Sahabi;
import com.omt.tarjimdinek.domain.member.entity.Membre;
import com.omt.tarjimdinek.support.Identifiable;

/**
 * user of social network
 * @author chabane
 *
 */
@Entity
@Table(name="a01_usr")
@FilterDef(name = "myAccountFilter", defaultCondition = "ID = :currentAccountId ", parameters = @ParamDef(name = "currentAccountId", type = "org.hibernate.type.StringType"))
@Filter(name = "myAccountFilter")
public class Utilisateur  implements Identifiable<Long>, Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(Utilisateur.class);
    
	/** ID SGBD */
	@Column(name="CNUSR", length=11)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(name = "VERSION", precision = 10)
    @Version
    private Integer version;
	
	/** Login */
	@Size(max = 45)
	@Column(name="LIUSR1", length=45, nullable=false, unique=true)
	@Email
	private String login;
	
	/** Password */
	@Size(max = 45)
	@Column(name="LIUSR2", length=45, nullable=false)
	private String password;
	
	/** Member attached */
	@NotNull
	@JoinColumn(name="CNMBR", nullable=false)
	@OneToOne
	private Membre membre;
	
	/** List of authorizations */
	@OneToMany(mappedBy="utilisateur", fetch = FetchType.LAZY)
	private Collection<Role> listeRoles;
	
	@JoinTable(name = "a01_usr_prm", 
			joinColumns = @JoinColumn(name = "CNUSR"), 
			inverseJoinColumns = @JoinColumn(name = "CNPRM"))
	@ManyToMany(fetch = FetchType.LAZY)
	private List<Parametrage> listeParametrages;

	@Column(name="BOENA", nullable=false)
	private Boolean enabled;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Membre getMembre() {
		return membre;
	}

	public void setMembre(Membre membre) {
		this.membre = membre;
	}

	public List<Parametrage> getListeParametrages() {
		return listeParametrages;
	}

	public void setListeParametrages(List<Parametrage> listeParametrages) {
		this.listeParametrages = listeParametrages;
	}
	
	public Collection<Role> getListeRoles() {
		return listeRoles;
	}

	public void setListeRoles(Collection<Role> listeRoles) {
		this.listeRoles = listeRoles;
	}
	
	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

    /**
     * Set the default values.
     */
    public void initDefaultValues() {
    }
    
	@Transient
	public boolean isIdSet() {
		return id != null && id.compareTo(Long.valueOf(0)) != 0;
	}
	
	/**
     * equals implementation using a business key.
     */
    public boolean equals(Object other) {
        return this == other || (other instanceof Sahabi && hashCode() == other.hashCode());
    }

    public int hashCode() {
        int hashCode = Objects.hashCode(getId());
        return hashCode;
    }
    
    /**
     * Construct a readable string representation for this instance.
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return Objects.toStringHelper(this) //
                .add("id", getId()) //
                .add("listeParametrages", getListeParametrages()) //
                .add("listeRoles", getListeRoles()) //
                .add("login", getLogin()) //
                .add("membre", getMembre()) //
                .add("enabled", getEnabled()) //
                .add("version", getVersion()) //
                .toString();
    }
}
